var GroupWindowType_See = 1;       //查看分组界面
var GroupWindowType_Grouping = 2;  //成员分组界面
var GroupWindowType_ChangeName = 3;//修改组名界面
var GroupWindowType_QuickGrouping = 4;//快速分配

/**
 * 分组选择界面
 *
 * 成员分组
 * 分组查看
 * 修改组名
 *
 * 都是这个类
 *
 *
 */
var ClubGroupWindow = Window.extend({
    _ccsFileName: res.ClubGroupWindow_json,
    _type: 1,
    _groupList: [],
    _clubId: 0,
    initWindow: function (type, clubId) {
        this._type = type;
        type = type == undefined ? 1 : type;
        this._nodeList['label_title1'].setVisible(GroupWindowType_See == type);
        this._nodeList['label_title2'].setVisible(GroupWindowType_Grouping == type);
        this._nodeList['label_title3'].setVisible(GroupWindowType_ChangeName == type);

        this._clubId = clubId;
        clubMsgManager.requestGroups(this._clubId);
        // this._groupList = ClubDataManager.getClubGroupsInfo(this._clubId);
        // this.refreshScrollView();
    },


    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.AddmodifyClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubMsgCode.AddClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubGroupSelectEvent] = '_selectGroupHandler';
    },

    /**
     * 分组信息请求回调
     * @param data
     * @private
     */
    _queryClubGroupHandler: function (data) {
        this._groupList = ClubDataManager.getClubGroupsInfo(this._clubId);
        if (this._groupList && this._groupList.length > 0) {
            this.refreshScrollView();
        }
    },

    /**
     * 选中组事件回调
     * @param data
     * @private
     */
    _selectGroupHandler: function (data) {
        //修改组名
        if (this._type == GroupWindowType_ChangeName) {
            showWindow('ClubGroupChangNameWindow', this._clubId, data);
        } else {
            this.close();
        }
    },


    /**
     * 初始化TableView
     */
    refreshScrollView: function () {
        var scrollView = this._nodeList['scrollView'];
        scrollView.removeAllChildren();

        // var groupList = this._groupList.clone();
        var groupList = _.cloneDeep(this._groupList);

        //查看分组界面 开头显示 全部分组 按钮
        if (this._type == GroupWindowType_See) {
            groupList.unshift({type: ClubGroupBtn_All});
        }
        //修改组名界面 结尾显示 增加分组 按钮
        if (this._type == GroupWindowType_ChangeName) {
            groupList.push({type: ClubGroupBtn_AddGroup});
        }

        //成员分组 和 修改组名 不显示未分组按钮
        if (this._type == GroupWindowType_QuickGrouping || this._type == GroupWindowType_ChangeName) {
            //修改组名界面 不能修改 未分组组名
            if (groupList[0].name == '未分组') {
                groupList.shift();
            }
        }


        var len = Math.ceil(groupList.length / 3);
        var tmpHeight = len * 90; // 90为ClubGroupItem对象的高度
        var innerHeight = Math.max(scrollView.getContentSize().height, tmpHeight);

        var index = 1;
        //每个ClubGroupItem 都是三个按钮
        for (var i = 0; i < groupList.length; i += 3) {
            var data = [
                groupList[i],
                groupList[i + 1],
                groupList[i + 2]
            ];
            var groupItem = addItem('ClubGroupItem', scrollView, data,this._type);
            groupItem.setPositionY(innerHeight - 90 * index);
            index++;
        }
        scrollView.innerHeight = innerHeight;
    }

});